﻿Public Class ThamSo
    Private status As Boolean = False
    Private data As DataTable
    Private dlieu As String
    Private xlts As XLThamSo
    Private ts As DTO_ThamSo
    Private vt As Integer = 0
    Private Sub ThamSo_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            Dim xl As New XLThamSo
            If xl.SaveTS(data) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub

    Private Sub ThamSo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        data = New DataTable
        Dim xl As New XLThamSo
        data = xl.LayDSTS
        showLV()
    End Sub

    Private Sub lv_thamso_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_thamso.SelectedIndexChanged
        Try
            dlieu = lv_thamso.SelectedItems(0).SubItems(0).Text
            vt = lv_thamso.SelectedItems(0).Index
            tb_manv.Text = lv_thamso.SelectedItems(0).SubItems(1).Text
            tb_tuoimin.Text = lv_thamso.SelectedItems(0).SubItems(2).Text
            tb_tuoimax.Text = lv_thamso.SelectedItems(0).SubItems(3).Text
            tb_luongcb.tb_nhapso.Text = lv_thamso.SelectedItems(0).SubItems(4).Text
            tb_NgayTra.tb_nhapso.Text = lv_thamso.SelectedItems(0).SubItems(5).Text
            tb_bhyt.Text = lv_thamso.SelectedItems(0).SubItems(6).Text
            tb_bhxh.Text = lv_thamso.SelectedItems(0).SubItems(7).Text
            tb_dk.tb_nhapso.Text = lv_thamso.SelectedItems(0).SubItems(8).Text
            tb_hs.Text = lv_thamso.SelectedItems(0).SubItems(9).Text
            status = True
        Catch ex As Exception
        End Try
    End Sub

    Private Sub bt_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_them.Click
        xlts = New XLThamSo
        ts = New DTO_ThamSo
        xlts.KtraThem(ts, tb_manv.Text, tb_tuoimin.Text, tb_tuoimax.Text, tb_luongcb.tb_nhapso.Text, tb_NgayTra.tb_nhapso.Text, tb_bhyt.Text, tb_bhxh.Text, tb_hs.Text, tb_dk.tb_nhapso.Text)
        Dim dt As DataRow = data.NewRow()
        db(dt)
        data.Rows.Add(dt)
        dh(dt)
        Refesh_()
    End Sub

    Private Sub bt_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_xoa.Click
        If status = True Then
            For Each dr As DataRow In data.Rows
                If dr.RowState <> DataRowState.Deleted Then
                    If dr("MaTS") = dlieu Then
                        dr.Delete() 'xoa du lieu tai dong so sánh
                        lv_thamso.Items.RemoveAt(vt)
                        Exit For
                    End If
                End If
            Next
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cập Nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_capnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_capnhat.Click
        If status = True Then
            xlts = New XLThamSo
            ts = New DTO_ThamSo
            For Each dr As DataRow In data.Rows
                If dr("MaTS") = dlieu Then
                    ' cap nhat lai trên listview
                    xlts.KtraThem(ts, tb_manv.Text, tb_tuoimin.Text, tb_tuoimax.Text, tb_luongcb.tb_nhapso.Text, tb_NgayTra.tb_nhapso.Text, tb_bhyt.Text, tb_bhxh.Text, tb_hs.Text, tb_dk.tb_nhapso.Text)
                    db(dr)
                    lv_thamso.Items(vt).SubItems(1).Text = ts.MaNV
                    lv_thamso.Items(vt).SubItems(2).Text = ts.TuoiMin
                    lv_thamso.Items(vt).SubItems(3).Text = ts.TuoiMax
                    lv_thamso.Items(vt).SubItems(4).Text = ts.LuongCB
                    lv_thamso.Items(vt).SubItems(5).Text = ts.NgTrLuong
                    lv_thamso.Items(vt).SubItems(6).Text = ts.BHYT
                    lv_thamso.Items(vt).SubItems(7).Text = ts.BHXH
                    lv_thamso.Items(vt).SubItems(8).Text = ts.DKTCN
                    lv_thamso.Items(vt).SubItems(9).Text = ts.TNCN
                End If
            Next
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cập Nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_lamlai.Click
        Refesh_()
    End Sub
    Private Sub showLV()
        If data.Rows.Count < 0 Then Return
        lv_thamso.Items.Clear()
        lv_thamso.View = View.Details
        lv_thamso.GridLines = True
        lv_thamso.FullRowSelect = True
        For Each dt As DataRow In data.Rows
            dh(dt)
        Next
    End Sub
    Private Sub CapNhatSTT()
        For i As Integer = 1 To lv_thamso.Items.Count
            lv_thamso.Items(i - 1).Text = i.ToString
        Next
    End Sub
    Private Sub Refesh_()
        tb_manv.Text = String.Empty
        tb_tuoimin.Text = String.Empty
        tb_tuoimax.Text = String.Empty
        tb_luongcb.tb_nhapso.Text = String.Empty
        tb_NgayTra.tb_nhapso.Text = String.Empty
        'tb_heso.Text = String.Empty
        tb_bhyt.Text = String.Empty
        tb_bhxh.Text = String.Empty
        tb_dk.tb_nhapso.Text = String.Empty
        tb_hs.Text = String.Empty
        dlieu = String.Empty
        status = False
    End Sub
    Private Sub dh(ByVal dt As DataRow)
        Dim item As New ListViewItem
        item.Text = "STT"
        item.SubItems.Add(dt("MaNV"))
        item.SubItems.Add(dt("TuoiMin"))
        item.SubItems.Add(dt("TuoiMax"))
        item.SubItems.Add(dt("LuongCB"))
        item.SubItems.Add(dt("NgTra"))
        item.SubItems.Add(dt("BHYT"))
        item.SubItems.Add(dt("BHXH"))
        item.SubItems.Add(dt("DKTNCN"))
        item.SubItems.Add(dt("TNCN"))
        lv_thamso.Items.Add(item)
        CapNhatSTT()
    End Sub
    Private Sub db(ByRef dt As DataRow)
        dt("MaNV") = ts.MaNV
        dt("TuoiMin") = ts.TuoiMin
        dt("TuoiMax") = ts.TuoiMax
        dt("LuongCB") = ts.LuongCB
        dt("NgTra") = ts.NgTrLuong
        dt("BHYT") = ts.BHYT
        dt("BHXH") = ts.BHXH
        dt("DKTNCN") = ts.DKTCN
        dt("TNCN") = ts.TNCN
    End Sub
End Class